from django.shortcuts import redirect
from django.utils.deprecation import MiddlewareMixin

from xiaozuzuoye import settings
from web import models


class AuthMiddleware(MiddlewareMixin):
    def process_request(self, request):

        user_id = request.session.get('user_id', 0)
        user_object = models.UserInfo.objects.filter(id=user_id).first()
        request.user_object = user_object

        if request.path_info in settings.WHITE_REGEX_URL_LIST:
            return
        if not user_object:
            return redirect('/web/login/')
